/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using DaveSexton.DocProject.Engine;
using DaveSexton.DocProject.Resources;
using System.Globalization;

namespace DaveSexton.DocProject
{
	public sealed class DocProjectFolder : DocProjectItemBase
	{
		#region Public Properties
		public bool DeleteRecursive
		{
			get
			{
				return deleteRecursive;
			}
		}

		public override bool Exists
		{
			get
			{
				return directory.Exists;
			}
		}

		/// <summary>
		/// Gets the path of the folder.
		/// </summary>
		/// <remarks>
		/// The path can be absolute or relative from the DocProject's configured <see cref="BuildSettings.OutputDirectory" />.
		/// </remarks>
		public override SystemPath Path
		{
			get
			{
				return directory;
			}
		}
		#endregion

		#region Private / Protected
		private readonly SystemPath directory;
		private readonly bool deleteRecursive;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="DocProjectFolder" /> class.
		/// </summary>
		/// <param name="path">Absolute or relative path from the DocProject's configured <see cref="BuildSettings.TargetDirectory" />.</param>
		/// <param name="missingBuildTypeDependency">Indicates the <see cref="BuildType" /> that can be executed to replace the item when the item is missing.</param>
		/// <param name="updatedBuildTypeDependency">Indicates the <see cref="BuildType" /> that is executed when the item has changed.</param>
		public DocProjectFolder(SystemPath directory, bool deleteRecursive, bool removeOnClean, bool includeInProject, 
			MissingBuildTypeDependency missingBuildTypeDependency, UpdatedBuildTypeDependency updatedBuildTypeDependency, bool isRequiredOutput)
			: base(removeOnClean, includeInProject, missingBuildTypeDependency, updatedBuildTypeDependency, isRequiredOutput)
		{
			if (directory == SystemPath.Empty)
				throw new ArgumentException(Resources.Errors.EmptySystemPathIsInvalid, "directory");

			if (!directory.IsDirectory)
				throw new ArgumentException(Resources.Errors.SystemPathNotDirectory, "directory");

			this.directory = directory;
			this.deleteRecursive = deleteRecursive;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="DocProjectFolder" /> class.
		/// </summary>
		/// <param name="path">Absolute or relative path from the DocProject's configured <see cref="BuildSettings.TargetDirectory" />.</param>
		/// <param name="missingBuildTypeDependency">Indicates the <see cref="BuildType" /> that can be executed to replace the item when the item is missing.</param>
		/// <param name="updatedBuildTypeDependency">Indicates the <see cref="BuildType" /> that is executed when the item has changed.</param>
		public DocProjectFolder(SystemPath directory, bool deleteRecursive, bool removeOnClean, bool includeInProject, 
			MissingBuildTypeDependency missingBuildTypeDependency, UpdatedBuildTypeDependency updatedBuildTypeDependency)
			: base(removeOnClean, includeInProject, missingBuildTypeDependency, updatedBuildTypeDependency)
		{
			if (directory == SystemPath.Empty)
				throw new ArgumentException(Resources.Errors.EmptySystemPathIsInvalid, "directory");

			if (!directory.IsDirectory)
				throw new ArgumentException(Resources.Errors.SystemPathNotDirectory, "directory");

			this.directory = directory;
			this.deleteRecursive = deleteRecursive;
		}
		#endregion

		#region Methods
		public override bool Delete(IAnyProject project)
		{
			if (project == null)
				throw new ArgumentNullException("project");
			
			try
			{
				project.GetFolder(directory.Path).Delete();
				return true;
			}
			catch (UnauthorizedAccessException ex)
			{
				Log.Exception(null, Errors.ProjectItemNotRemovedLogMessage, Environment.NewLine, directory, ex.Message);
			}
			catch (PathTooLongException ex)
			{
				Log.Exception(null, Errors.ProjectItemNotRemovedLogMessage, Environment.NewLine, directory, ex.Message);
			}
			catch (DirectoryNotFoundException ex)
			{
				Log.Exception(null, Errors.ProjectItemNotRemovedLogMessage, Environment.NewLine, directory, ex.Message);
			}
			catch (IOException ex)
			{
				Log.Exception(null, Errors.ProjectItemNotRemovedLogMessage, Environment.NewLine, directory, ex.Message);
			}

			return false;
		}
		#endregion
	}
}
